//
//  ZLExtension.swift
//  ZLAlert
//
//  Created by ZL on 2022/11/24.
//

import UIKit

extension UIViewController {
    /// 获取最顶层控制器
    /// - Parameters:
    ///   - controller: 控制器
    /// - Returns: UIViewController?
    class func zl_topViewController(_ controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            if let visible = navigationController.visibleViewController {
                return zl_topViewController(visible)
            }
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return zl_topViewController(selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return zl_topViewController(presented)
        }
        return controller
    }
}

/// 视图
extension UIView {
    /// 宽度
    var zl_width: CGFloat {
        get {
            return self.frame.size.width
        }
    }
    /// 高度
    var zl_height: CGFloat {
        get {
            return self.frame.size.height
        }
    }
    /// 底部
    var zl_bottom: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
    }
}

///  标题
extension UILabel {
    class func zl_label(_ config: ZLAlertItemConfig) -> UILabel {
        let label = UILabel()
        label.font = config.font
        label.textColor = config.color
        label.textAlignment = config.alignment
        label.backgroundColor = config.backgroundColor
        label.layer.cornerRadius = config.cornerRadius
        label.layer.borderWidth = config.borderWidth
        label.layer.borderColor = config.borderColor.cgColor
        label.layer.masksToBounds = true
        return label
    }
}

/// 按钮
public typealias ZlAlertAction = () -> Void
public class ZLAlertButton: UIButton {
    /// 事件
    var action: ZlAlertAction?
    /// 点击是否自动消失
    var dismiss: Bool = false
    /// 是否调用TextField
    var callTextField: Bool = false
    /// 创建按钮
    /// - Parameters:
    ///   - title: 标题
    ///   - dismiss: 点击是否自动消失
    ///   - config: 配置
    ///   - callTextField: 是否调用TextField
    ///   - action: 事件
    /// - Returns: ZLAlertButton
    class func zl_button(_ title: String, _ dismiss: Bool, _ config: ZLAlertItemConfig, _ callTextField: Bool = false, _ action: ZlAlertAction?) -> ZLAlertButton {
        let button = ZLAlertButton()
        button.layer.cornerRadius = config.cornerRadius
        button.layer.borderWidth = config.borderWidth
        button.layer.borderColor = config.borderColor.cgColor
        button.layer.masksToBounds = true
        button.setTitleColor(config.color, for: .normal)
        button.titleLabel?.font = config.font
        button.titleLabel?.textAlignment = config.alignment
        button.backgroundColor = config.backgroundColor
        button.setTitle(title, for: .normal)
        button.dismiss = dismiss
        button.action = action
        button.callTextField = callTextField
        switch (config.alignment) {
        case .left:
            button.contentHorizontalAlignment = .left
            break
        case .right:
            button.contentHorizontalAlignment = .right
            break
        default:
            break
        }
        return button
    }
}

/// 颜色
extension UIColor {
    /// 十六进制颜色
    /// - Parameters:
    ///   - hexString: 十六进制字符串
    ///   - alpha: 透明度
    /// - Returns: UIColor
    class func zl_hex(_ hexString: String, alpha: CGFloat = 1.0) -> UIColor {
       var hex = hexString.hasPrefix("#") ? String(hexString.dropFirst()) : hexString
       guard hex.count == 3 || hex.count == 6  else {
           return UIColor.clear
       }
       
       if hex.count == 3 {
           for (indec, char) in hex.enumerated() {
               hex.insert(char, at: hex.index(hex.startIndex, offsetBy: indec * 2))
           }
       }
       
       let color = UIColor.init(
           red: CGFloat((Int(hex, radix: 16)! >> 16) & 0xFF) / 255.0,
           green: CGFloat((Int(hex, radix: 16)! >> 8) & 0xFF) / 255.0,
           blue: CGFloat((Int(hex, radix: 16)!) & 0xFF) / 255.0,
           alpha: alpha
       )
       return color
   }
}
